package jp.co.nes.awssupport.compile.function;

import java.time.YearMonth;
import java.util.List;

import jp.co.nes.awssupport.access.dao.MonthlyCompiledBillingDao;
import jp.co.nes.awssupport.access.dao.MonthlyCompiledBillingDao.DaoReadTypeOfTotalization;
import jp.co.nes.awssupport.access.dao.TaxAssignedDao;
import jp.co.nes.awssupport.business.base.AbstractRequestWithCompletionStateHandler;
import jp.co.nes.awssupport.business.bean.CommonContainerBean.ProcessingResult;
import jp.co.nes.awssupport.business.bean.CompileContainerBean;
import jp.co.nes.awssupport.business.bean.CompileInfoBean;
import jp.co.nes.awssupport.business.logic.TaxPlanProcessing;
import jp.co.nes.awssupport.common.bean.MonthlyCompiledBillingBean;
import jp.co.nes.awssupport.common.bean.TaxAssignedBean;
import jp.co.nes.awssupport.common.dao.ReadHandler;
import jp.co.nes.awssupport.common.dao.ReadType;
import jp.co.nes.awssupport.common.utility.BeanUtil;
import jp.co.nes.awssupport.common.utility.DateUtil;

/**
 * 税金プラン適用です(Lambda)。
 */
public class TaxPlan
        extends AbstractRequestWithCompletionStateHandler<CompileInfoBean, CompileContainerBean<CompileInfoBean>> {

    /**
     * デフォルトコンストラクタです。
     */
    public TaxPlan() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.base.AbstractRequestHandler#handleRequest(java.lang.Object)
     */
    @Override
    protected CompileContainerBean<CompileInfoBean> handleRequest(CompileInfoBean input) {
        YearMonth targetYearMonth = DateUtil.toYearMonth(input.getTargetYearMonthValue());
        String linkedAccountId = input.getLinkedAccountId();

        // TaxPlanProcessingの生成はread()メソッドの外で実行する(契約情報の取得を複数回実行しないため)
        TaxPlanProcessing tpProcessing = new TaxPlanProcessing(targetYearMonth);

        // 検索条件の作成
        Object[] conditions = new Object[2];
        conditions[0] = linkedAccountId;
        conditions[1] = DateUtil.toInt(targetYearMonth);

        Class<MonthlyCompiledBillingBean> cls = MonthlyCompiledBillingBean.class;
        MonthlyCompiledBillingDao dao = new MonthlyCompiledBillingDao();
        ReadType readType = DaoReadTypeOfTotalization.TOTALIZATION_FF;
        dao.read(new ReadHandler<MonthlyCompiledBillingBean>(cls, readType) {
            /*
             * (非 Javadoc)
             * @see jp.co.nes.awssupport.common.dao.ReadHandler#read(jp.co.nes.awssupport.common.bean.BaseBean)
             */
            @Override
            protected void read(MonthlyCompiledBillingBean mcbBean) {
                // 税金プランを適用
                List<TaxAssignedBean> applyList = tpProcessing.apply(mcbBean);

                // 税金適用を削除・登録
                TaxAssignedDao assignedDao = new TaxAssignedDao();
                assignedDao.delete(mcbBean.getLinkedAccountId(), targetYearMonth);
                assignedDao.insert(applyList);
            }
        }, conditions);

        //CompileProgressManage cpManage = new CompileProgressManage(targetYearMonth);
        //cpManage.completion(input.getLinkedAccountId(), CompletionState.TAX_PLAN);

        // 返却内容の設定
        CompileInfoBean info = new CompileInfoBean();
        BeanUtil.copyProperties(info, input);
        CompileContainerBean<CompileInfoBean> output = new CompileContainerBean<>();
        output.setInfoBean(info);
        output.setProcessingResult(ProcessingResult.SUCCESS);
        return output;
    }
}
